<?php
namespace Home\Controller;
use Think\Controller;
class AlipayController extends Controller {
	function do_alipay(){
	require_once(  './third_party/alipay/alipay_submit.class.php' );
		//构造要请求的参数数组，无需改动
		$total_fee = $_GET['money'] / 100;
		//$total_fee = str_replace(',', '',number_format(intval( $total_fee ), 2));
		$ext1 = $_GET['ext1'];  //3.押金充值  4.金币充值
		$ext2 = $_GET['ext2'];  //充值的金额*100的值，去除小数点

		$subject = "用户充值";
		//$total_fee = 0.01;
		if(isset($_SESSION['user_id'])) {
			$user_id = session('name','value');
			
		} else {
			// $user_id = $this->session->userdata('seller_id');
			//$body = '商家充值:'. $total_fee . "元";
		}
		$out_trade_no = 'ZFB' . date('YmdHis') . sprintf("%06d", $user_id);
		$parameter = array(
			"service"        => $this->alipay_config['service'],
			"partner"        => trim($this->alipay_config['partner']),
			"payment_type"   => $this->alipay_config['payment_type'],
			"notify_url"     => $this->alipay_config['notify_url'],
			"return_url"     => $this->alipay_config['return_url'],
			"seller_id"      => $this->alipay_config['partner'],  //支付宝帐户,
			"out_trade_no"   => $out_trade_no,                    //商户订单号
			"subject"        => $subject,                         //订单名称
			"total_fee"      => $total_fee,                       //必填,付款金额
			"body"           => $ext1.'-'.$user_id.'-'. $ext2,               //必填,订单描述
			"_input_charset" => trim(strtolower($this->alipay_config['input_charset']))
		);

		//P($parameter);exit;
		//建立请求
        $alipaySubmit = new AlipaySubmit($this->alipay_config);
		$html_text = $alipaySubmit->buildRequestForm($parameter, "post", "确认");
		
		echo $html_text;
	}
    
 	private function _init_config(){
		//合作身份者ID，签约账号，以2088开头由16位纯数字组成的字符串，查看地址：https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
		$alipay_config['partner']		= '2088421590378083';

		//收款支付宝账号，以2088开头由16位纯数字组成的字符串，一般情况下收款账号就是签约账号
		$alipay_config['seller_id']	= $alipay_config['partner'];

		//商户的私钥,此处填写原始私钥去头去尾，RSA公私钥生成：https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.nBDxfy&treeId=58&articleId=103242&docType=1
		//$alipay_config['private_key']	= 'MIICXAIBAAKBgQC57OnWpddz90Fu7K7c800f9ssenh+/NAyRoPA48AU2XFCHVcpZIdHoUFEWcUsgY6WncAkz5pwIzmIDyTagCWxWor3+WcWrN/KCFZYBzyov8dbWa46qhdSpN9HNH8ruJkHzGzTln4OHiJbgJJSs6zNrTfQMClQRfQQoRQd4b1X8XQIDAQABAoGAGPzmUJrpYgSRHr9goQFyFCGvGuZVjfUI22mt33kiujOd/xJgp6oL9BUXUNTZK/k4glBkbD1KK0TRl4CrfsbFcISDM1111zRaDAC8ZLZ9X5hkJO7aOjezBc8ZGhIqjJz89dhiEIksbsW8Mnlv4kYjsGc9t9b0KLBvvSKO6blsCAECQQDtqJiIGnvtL5DyasXqu1zOzvYFSmvqMd54xXm2DPOudgD7JHobFlUfF+kQGeSV0h1Z6hEByNhz9loEVY2ctHTBAkEAyEY6yMxMhntRFedp2L8ZMGhyzCIIm7dTZ2vbG3uDqnB8FZqB+irudFaAVUhzhc13fQuiiqLzJaQU3uuwc5jinQJABFkUSVEquUEohrbbXhsFbKcuqxQce1FkP5NlUaZVAvkXtYCKV1qGPiZi1i+TFgadENsOapjxFNYJ+ZBTIe3PQQJAH7bPVEDV1W3ttzRAFKyeRoYjt6OkDVSIhpYTylRS00igvaML9huaSx3vkJG6iXOx+kE/agjHzCwhifN0q7mIGQJBAMOdQovx0soYaQP5J1pAUVu7oXpotfiI/+okpqn04t7MnHtFnJViNQHBu3V7CBogKiBMCBQTwwdto+EZwmWp2os=';

		//支付宝的公钥，查看地址：https://b.alipay.com/order/pidAndKey.htm 
		//$alipay_config['alipay_public_key']= 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC57OnWpddz90Fu7K7c800f9ssenh+/NAyRoPA48AU2XFCHVcpZIdHoUFEWcUsgY6WncAkz5pwIzmIDyTagCWxWor3+WcWrN/KCFZYBzyov8dbWa46qhdSpN9HNH8ruJkHzGzTln4OHiJbgJJSs6zNrTfQMClQRfQQoRQd4b1X8XQIDAQAB';

		// 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
		$alipay_config['notify_url'] = site_url('alipay/do_notify');

		// 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
		$alipay_config['return_url'] = site_url('alipay/do_return');
		
		$alipay_config['key']			= '2hsa7j04dq8thxxldpju1ven1yp341je';

		//签名方式
		//$alipay_config['sign_type']    = strtoupper('RSA');
		$alipay_config['sign_type']    = strtoupper('MD5');

		//字符编码格式 目前支持 gbk 或 utf-8
		$alipay_config['input_charset']= strtolower('utf-8');

		//ca证书路径地址，用于curl中ssl校验
		//请保证cacert.pem文件在当前文件夹目录中
		//$alipay_config['cacert']    = getcwd().'\\rsa_public_key.pem';
		$alipay_config['cacert']    = APPPATH.'third_party/alipay/rsa_public_key.pem';

		//访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
		$alipay_config['transport']    = 'http';

		// 支付类型 ，无需修改
		$alipay_config['payment_type'] = "1";

		// 产品类型，无需修改
		$alipay_config['service'] = "create_direct_pay_by_user";

		//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

		//↓↓↓↓↓↓↓↓↓↓ 请在这里配置防钓鱼信息，如果没开通防钓鱼功能，为空即可 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

		// 防钓鱼时间戳  若要使用请调用类文件submit中的query_timestamp函数
		$alipay_config['anti_phishing_key'] = "";

		// 客户端的IP地址 非局域网的外网IP地址，如：221.0.0.1
		$alipay_config['exter_invoke_ip'] = ""; 

		$this->alipay_config = $alipay_config;
	}
}